package com.ruanda.controller; /**
 * 会员收藏商品业务控制层
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.MemberCollection;
import com.ruanda.domain.Prod;
import com.ruanda.model.Result;
import com.ruanda.service.MemberCollectionService;
import com.ruanda.util.AuthUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "会员收藏商品接口管理")
@RequestMapping("p/collection")
@RestController
public class MemberCollectionController {
    @Autowired
    private MemberCollectionService memberCollectionService;

    @Operation(summary = "查询会员收藏商品的数量")
    @GetMapping("count")
    public Result<Long> loadMemberCollectionProdCount() {
        Long count = memberCollectionService.queryMemberCollectionProdCount();
        return Result.success(count);
    }

    @Operation(summary = "分页查询会员收藏商品列表")
    @GetMapping("prods")
    public Result<Page<Prod>> LoadMemberCollectionProdPage(@RequestParam Long current,
                                                           @RequestParam Long size) {
        // 根据会员openid分页查询会员收藏商品列表
        Page<Prod> page = memberCollectionService.queryMemberCollectionProdPageByOpenId(AuthUtils.getMemberOpenId(), current, size);
        return Result.success(page);
    }

    
    ///////////// 小程序 /////////////
    @Operation(summary = "小程序查询会员收藏商品状态")
    @GetMapping("isCollection")
    public Result<Boolean> loadMemberIsCollection(@RequestParam Long prodId) {
        Long count = memberCollectionService.count(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, AuthUtils.getMemberOpenId())
                .eq(MemberCollection::getProdId, prodId)
        );
        return Result.success(1 == count);
    }

    @Operation(summary = "添加或取消收藏")
    @PostMapping("addOrCancel")
    public Result<String> addOrCancelCollect(@RequestBody Long prodId) {
        Integer count = memberCollectionService.addOrCancelCollect(AuthUtils.getMemberOpenId(),prodId);
        return Result.handle(count>0);
    }
}